<?php


namespace app\cms\admin\controller;


use app\cms\model\adminUserModel;
use app\cms\model\adminUseRoleModel;
use app\cms\model\sysRoleModel;
use app\cms\model\userModel;
use app\cms\model\userRoleModel;
use iflow\auth\Authorize\AuthAnnotation;
use iflow\Response;
use iflow\Router\Controller;
use iflow\Router\implement\Request\{ GetMapping, PostMapping, DeleteMapping };

#[Controller('%cmsAdmin%/user')]
class user {

    public userModel $userModel;
    public userRoleModel $userRoleModel;

    public function __construct() {
        $model = request() -> params('model');
        $this->userModel = new ($model === 'admin' ? adminUserModel::class : userModel::class);
        $this->userRoleModel = new ($model === 'admin' ? adminUseRoleModel::class : userRoleModel::class);
    }

    /**
     * 获取用户列表
     * @param int $user_group_id
     * @param string $user_name
     * @return Response
     * @throws \think\db\exception\DbException
     */
    #[GetMapping]
    #[AuthAnnotation('user', 'userReadWrite|userRead')]
    public function getUserList(int $user_group_id = 0, string $user_name = ''): Response
    {
        return $this->userModel -> getUserList($user_group_id, $user_name);
    }

    /**
     * 获取指定用户信息
     * @param int $user_id
     * @return Response
     */
    #[GetMapping]
    #[AuthAnnotation('user', 'userReadWrite|userRead')]
    public function getUserInfo(int $user_id = 0): Response
    {
        $userInfo = $this->userModel -> getUser($user_id);
        return $userInfo ?
            message() -> success('查询成功', $userInfo): message() -> nodata('暂无该用户');
    }

    /**
     * 获取指定用户权限
     * @param int $user_id
     * @return Response
     */
    #[GetMapping('get_user_role/<[0-9]{1,8}:user_id>')]
    #[AuthAnnotation('user', 'userReadWrite|userRead')]
    public function getUserRole(int $user_id): Response
    {
        $userInfo = $this->userRoleModel -> getUserRoleList($user_id);
        return $userInfo ?
            message() -> success('获取用户权限成功', $userInfo) : message() -> nodata('暂无权限数据');
    }

    /**
     * 设置用户角色权限
     * @param int $id
     * @param int $user_role_id
     * @return Response
     */
    #[PostMapping]
    #[AuthAnnotation('user', 'userReadWrite|userWrite')]
    public function setUserRole(int $id, int $user_role_id): Response
    {
        return $this->userRoleModel -> setUserRoles($id, $user_role_id, 'uid');
    }

    /**
     * 获取 用户角色列表
     * @param string $user_role_name
     * @return Response
     */
    #[GetMapping]
    #[AuthAnnotation('user', 'userReadWrite|userRead')]
    public function getSysRoleList(string $user_role_name = ""): Response
    {
        return (new sysRoleModel()) -> getRoleList($user_role_name);
    }

    /**
     * 添加站点权限
     * @param string $user_role_name
     * @param string $user_role_path
     * @param string $user_role_description
     * @return Response
     */
    #[PostMapping]
    #[AuthAnnotation('user', 'userReadWrite|userWrite')]
    public function addSysRoleInfo(string $user_role_name = "", string $user_role_path = "", string $user_role_description = ""): Response
    {
        return (new sysRoleModel()) -> addRoleInfo($user_role_name, $user_role_path, $user_role_description);
    }

    /**
     * 删除站点权限
     * @param int $user_role_id
     * @return Response
     */
    #[DeleteMapping('delete_sys_role/<[0-9]{1,8}:user_role_id>')]
    #[AuthAnnotation('user', 'userReadWrite|userWrite')]
    public function deleteSysRole(int $user_role_id): Response
    {
        return (new sysRoleModel()) -> deleteRole($user_role_id);
    }

    /**
     * (解)封禁指定账户
     * @param int $uid
     * @return Response
     */
    #[PostMapping]
    #[AuthAnnotation('user', 'userReadWrite|userWrite')]
    public function changeUserStatus(int $uid = 0): Response
    {
        return $this->userModel -> changeUserStatus($uid);
    }

    /**
     * 移除用户指定权限
     * @param int $id
     * @param int $user_role_id
     * @return Response
     */
    #[
        DeleteMapping('delete_role/<[0-9]{1,8}:user_id>/<[0-9]{1,8}:user_role_id>'),
        AuthAnnotation('user', 'userReadWrite|userWrite')
    ]
    public function deleteRole(int $id, int $user_role_id): Response
    {
        return $this->userRoleModel -> deleteRole($id, $user_role_id, 'uid');
    }
}